/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.ServiceSetting;
import com.ybkj.daijia.serverUtils.ServiceUtil;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.RateMapper;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.Rate;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.RateService;
import com.ybkj.daijia.server.service.ZhuancheService;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 */
@Service
public class DefaultRateService implements RateService {

    @Autowired
    private RateMapper rateMapper;

    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private FreightService freightService;
    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private ErrandService errandService;
    @Autowired
    private OrderMapper orderMapper;

    /*
     * (non-Javadoc)
     *
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<Rate> queryAll(final Long driverid, final Date start,
        final Date end) {
        ServiceSetting checkOpen = ServiceUtil.checkOpen();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        List<Rate> content = new LinkedList<Rate>();
        long total = 0l;
        //屏蔽原因 --> 后台添加的评价会存在这张表，如果没开启代驾业务，添加的评价将查询不出
        //if (checkOpen.isDaijia()) {
        List<Rate> list = rateMapper.selectByDriver(driverid, start, end,
            Pager.getOffset(), Pager.getLimit());
        content.addAll(list);
        total += rateMapper.countByDriverAndDate(driverid, start, end);
        // }
        if (checkOpen.isFreight()) {
            Map<String, Object> map = freightService
                .findRate(driverid, Pager.getPage(), Pager.getLimit());
            content.addAll((List<Rate>) map.get("data"));
            total += (long) map.get("total");
        }
        if (checkOpen.isZhuanche()) {
            Map<String, Object> map = zhuancheService
                .findRate(driverid, Pager.getPage(), Pager.getLimit());
            content.addAll((List<Rate>) map.get("data"));
            total += (long) map.get("total");
        }
        if (checkOpen.isPaotui()) {
            Map<String, Object> map = errandService
                .findRate(driverid, Pager.getPage(), Pager.getLimit());
            content.addAll((List<Rate>) map.get("data"));
            total += (long) map.get("total");
        }

        return new PageImpl<Rate>(content, pageable, total);
    }

    @Override
    public Page<Rate> queryAll(final String param) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        if (StringUtils.isNotEmpty(param)) {
            List<Rate> content = rateMapper.selectByNameLike("%" + param + "%",
                Pager.getOffset(), Pager.getLimit());

            long total = rateMapper.countByNameLike("%" + param + "%");

            return new PageImpl<Rate>(content, pageable, total);
        } else {
            List<Rate> content = rateMapper.selectByNameLike(null,
                Pager.getOffset(), Pager.getLimit());

            long total = rateMapper.countByNameLike(null);

            return new PageImpl<Rate>(content, pageable, total);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Rate insertRecord(Rate rate) {

        rateMapper.insertRecord(rate);

        return rate;
    }

    public Rate findOne(Long id) {

        return rateMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void rate(Rate rate, Driver driver, Order order) {

        rateMapper.insertRecord(rate);
        order.setHasbeenRate(true);
        orderMapper.updateByPrimaryKey(order);

        if (null != driver) {

            double totalstar = rateMapper.sumScoreByDriver(driver.getId());
            long totalrate = rateMapper.countByDriver(driver.getId());
            if (totalrate > 30) {
                totalrate -= 30;
                totalstar -= rateMapper.sumScoreByDriverLimit30(driver.getId());
                DecimalFormat df = new DecimalFormat("#.0");
                driver.setStarLevel(Double.valueOf(df.format(totalstar / totalrate)));
                driverMapper.updateByPrimaryKey(driver);
            }

        }

    }

    @Override
    public Rate findByMemberAndOrder(Long memberId, Long orderId) {

        return rateMapper.selectByMemberAndOrder(memberId, orderId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Rate rate) {

        rateMapper.deleteByPrimaryKey(rate.getId());

    }

    @Override
    public Page<Rate> pageRateByDriver(Long driverId, Integer page,
        Integer limit) {

        Pageable pageable = new PageRequest(page, limit);

        List<Rate> content = rateMapper.selectByDriver(driverId, null, null,
            pageable.getOffset(), limit);
        long total = rateMapper.countByDriver(driverId);

        return new PageImpl<Rate>(content, pageable, total);
    }

}